// --------------------------------------------------------------------------------------------------------------------
// <copyright file="Bootstrapper.cs" company="Concept Architecture">
//   (c) 2010 Muhammad Umer Farooq
// </copyright>
// <summary>
//   Defines the <seealso cref="Bootstrapper" /> type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Hisaab.Client.Desktop
{
    using System.Configuration;
    using System.Windows;

    using AvalonDock;
    using Fluent;

    using Hisaab.Business.Interfaces;
    using Hisaab.Client.Common.Adapters;
    using Hisaab.Client.Common.Services;
    using Hisaab.Client.Desktop.View;

    using Microsoft.Practices.Prism.Modularity;
    using Microsoft.Practices.Prism.Regions;
    using Microsoft.Practices.Prism.UnityExtensions;
    using Microsoft.Practices.ServiceLocation;
    using Microsoft.Practices.Unity.Configuration;

    using Stema.Controls;

    /// <summary>
    /// Defines the <seealso cref="Bootstrapper"/> type.
    /// </summary>
    public class Bootstrapper : UnityBootstrapper
    {
        #region Method Overrides

        /// <summary>
        /// Populates the Module Catalog.
        /// </summary>
        /// <returns>A new Module Catalog.</returns>
        /// <remarks>
        /// This method uses the Module Discovery method of populating the Module Catalog. It requires
        /// a post-build event in each module to place the module assembly in the module catalog
        /// directory.
        /// </remarks>
        protected override IModuleCatalog CreateModuleCatalog()
        {
            var moduleCatalog = new DirectoryModuleCatalog { ModulePath = "./Modules" };
            return moduleCatalog;
        }

        /// <summary>
        /// Configures the default region adapter mappings to use in the application, in order
        /// to adapt UI controls defined in XAML to use a region and register it automatically.
        /// </summary>
        /// <returns>The RegionAdapterMappings instance containing all the mappings.</returns>
        protected override RegionAdapterMappings ConfigureRegionAdapterMappings()
        {
            // Call base method
            var mappings = base.ConfigureRegionAdapterMappings();
            if (mappings == null)
            {
                return null;
            }

            AddMappings(mappings);

            // Set return value
            return mappings;
        }

        /// <summary>
        /// Instantiates the Shell window.
        /// </summary>
        /// <returns>A new ShellWindow window.</returns>
        protected override DependencyObject CreateShell()
        {
            var session = ServiceLocator.Current.GetInstance<ISession>();
            var userId = session.LoggedUser;
            var userCurrencyLocale = ServiceLocator.Current.GetInstance<IUserManager>().GetCurrencySymbol(userId);
            session.SetCurrency(userCurrencyLocale);

            return new Shell();
        }

        /// <summary>
        /// Configures the container.
        /// </summary>
        protected override void ConfigureContainer()
        {
            base.ConfigureContainer();

            var section = (UnityConfigurationSection)ConfigurationManager.GetSection("unity");
            Container.LoadConfiguration(section, "default");
        }

        /// <summary>
        /// Displays the Shell window to the user.
        /// </summary>
        protected override void InitializeShell()
        {
            base.InitializeShell();

            ServiceLocator.Current.GetInstance<ISession>().Theme = Themes.Silver;

            Application.Current.MainWindow = (Window)Shell;
            Application.Current.MainWindow.Show();
        }

        /// <summary>
        /// Adds the mappings.
        /// </summary>
        /// <param name="mappings">The mappings.</param>
        private static void AddMappings(RegionAdapterMappings mappings)
        {
            mappings.RegisterMapping(typeof(Ribbon), ServiceLocator.Current.GetInstance<RibbonRegionAdapter>());
            mappings.RegisterMapping(typeof(RibbonGroupBox), ServiceLocator.Current.GetInstance<GroupRegionAdapter>());
            mappings.RegisterMapping(typeof(BackstageTabControl), ServiceLocator.Current.GetInstance<BackstageTabControlRegionAdapter>());
            mappings.RegisterMapping(typeof(NavigationPane), ServiceLocator.Current.GetInstance<NavigationPaneAdapter>());
        }

        #endregion Method Overrides
    }
}